package com.ruoyi.qymlxin.service.impl;

import java.util.List;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.qymlxin.esspringboot.BookService;
import com.ruoyi.qymlxin.util.UrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.qymlxin.mapper.IShopmsgMapper;
import com.ruoyi.qymlxin.domain.IShopmsg;
import com.ruoyi.qymlxin.service.IIShopmsgService;

import javax.annotation.Resource;

/**
 * 商品信息Service业务层处理
 *
 * @author qymlxin
 * @date 2022-05-03
 */
@Service
public class IShopmsgServiceImpl implements IIShopmsgService
{
    @Resource
    private IShopmsgMapper iShopmsgMapper;

    @Resource
    private UrlUtil urlUtil;

    @Resource
    private BookService bookService;

    /**
     * 查询商品信息
     *
     * @param id 商品信息主键
     * @return 商品信息
     */
    @Override
    public IShopmsg selectIShopmsgById(Long id)
    {
        return iShopmsgMapper.selectIShopmsgById(id);
    }

    /**
     * 查询商品信息列表
     *
     * @param iShopmsg 商品信息
     * @return 商品信息
     */
    @Override
    public List<IShopmsg> selectIShopmsgList(IShopmsg iShopmsg)
    {
        return iShopmsgMapper.selectIShopmsgList(iShopmsg);
    }

    /**
     * 新增商品信息
     *
     * @param iShopmsg 商品信息
     * @return 结果
     */
    @Override
    public int insertIShopmsg(IShopmsg iShopmsg)
    {
        String url = iShopmsg.getImgurl();
        String urlString = urlUtil.getUrlString(url);
        iShopmsg.setImgurl(urlString);
        String s = IdUtils.simpleUUID();
        iShopmsg.setShopid(s);
        int i = iShopmsgMapper.insertIShopmsg(iShopmsg);
        List<IShopmsg> iShopmsgs = iShopmsgMapper.selectIShopmsgList(iShopmsg);
        bookService.addBook(iShopmsgs.get(0));
        return i;
    }

    /**
     * 修改商品信息
     *
     * @param iShopmsg 商品信息
     * @return 结果
     */
    @Override
    public int updateIShopmsg(IShopmsg iShopmsg)
    {
        bookService.addBook(iShopmsg);
        return iShopmsgMapper.updateIShopmsg(iShopmsg);
    }

    /**
     * 批量删除商品信息
     *
     * @param ids 需要删除的商品信息主键
     * @return 结果
     */
    @Override
    public int deleteIShopmsgByIds(Long[] ids)
    {
        for (Long id : ids) {
            IShopmsg iShopmsg = iShopmsgMapper.selectIShopmsgById(id);
            bookService.deleteBook(iShopmsg);
        }
        return iShopmsgMapper.deleteIShopmsgByIds(ids);
    }

    /**
     * 删除商品信息信息
     *
     * @param id 商品信息主键
     * @return 结果
     */
    @Override
    public int deleteIShopmsgById(Long id)
    {
        IShopmsg iShopmsg = iShopmsgMapper.selectIShopmsgById(id);
        bookService.deleteBook(iShopmsg);
        return iShopmsgMapper.deleteIShopmsgById(id);
    }
}